package com.itts.hk.theard;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadUtils {
    public static void main(String[] args) {
        final ArrayList<Object> objects = new ArrayList<>();
        //todo 创建一个可以无限扩大的线程池，适用于负载较轻的场景，执行短期异步任务
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        /*for (int i = 0; i < 1000; i++) {

            cachedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+"执行");
                }
            });
        }*/
        cachedThreadPool.shutdown();//关闭线程池

        //todo 创建一个固定大小的线程池，因为采用无界的阻塞队列，所以实际线程数量永远不会变化，
        // 适用于负载较重的场景，对当前线程数量进行限制。（保证线程数可控，不会造成线程过多，导致系统负载更为严重）
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 1000; i++) {
            fixedThreadPool.execute(() -> {
                System.out.println(Thread.currentThread().getName() + "执行");
            });
        }
        fixedThreadPool.shutdown();//关闭线程池
    }
}
